<template>
  <div class="publish-container">
    <h2 style="text-align: center">评价关系设置</h2>
    <div class="relation-box">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
        <el-form-item label="部门名称" prop="name" v-if="type == 5">
          <el-cascader
            v-model="queryParams.evaluated"
            :options="orgList"
            :props="defaultProps"
            :clearable="true"
            size="small"
          />
        </el-form-item>
        <el-form-item label="被评价人" prop="name" v-if="type == 6">
          <el-input
            v-model="queryParams.evaluatedName"
            placeholder="请输入被评人姓名"
            clearable
            @keyup.enter.native="handleQuery"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"> 搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"> 重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd"> 新增</el-button>
          <el-button type="primary" icon="el-icon-upload" size="small" @click="handleImport"> 导入</el-button>
          <el-button type="primary" icon="el-icon-download" size="small" @click="handleExport"> 导出 </el-button>
        </el-col>
      </el-row>
      <el-table :data="dataList" v-loading="loading">
        <el-table-column prop="evaluatedName" :label="firstColumn" align="center"></el-table-column>
        <el-table-column label="评价人" align="left">
          <template slot-scope="scope">
            <el-tag v-for="e in scope.row.evaluators" style="margin-left: 3px" :key="e.id">
              {{
                scope.row.evaluators.filter((f) => f.name === e.name).length > 1
                  ? e.name + '(' + e.orgName + ')' + (e.userRelationName ? '-' + e.userRelationName : '')
                  : e.name + (e.userRelationName ? '-' + e.userRelationName : '')
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"> 修改 </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <relationship-modal ref="modal" @ok="submitOk"></relationship-modal>
    <import-modal ref="import" :form-key="formKey" :form-type="type" @success="importSuccess"></import-modal>
  </div>
</template>

<script>
import { getRequest, deleteRequest } from '@/api/baseRequest'
import Pagination from '@/components/Pagination'
import RelationshipModal from '@/views/form/relationship/RelationshipModal.vue'
import ImportModal from '@/views/form/relationship/ImportModal.vue'
import { downloadRelationshipExcel } from '@/api/project/relationship'

export default {
  name: 'index',
  components: { Pagination, RelationshipModal, ImportModal },
  computed: {
    firstColumn() {
      console.log(this.type)
      if (this.type == 5) {
        return '被评价部门'
      }
      if (this.type == 6) {
        return '被评价人'
      }
      return ''
    }
  },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name',
        checkStrictly: true,
        value: 'id'
      },

      total: 0,
      formKey: '',
      dataList: [],
      loading: false,
      url: {
        list: '/form/relationship/listAgg',
        delete: '/form/relationship/delete',
        orgList: '/org/tree'
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 显示搜索条件
      showSearch: true,
      orgList: [],
      type: ''
    }
  },
  created() {
    this.formKey = this.$route.query.key || this.$route.params.key
    this.type = this.$route.query.type || this.$route.params.type
    this.getList()
    this.getTreeList()
  },
  methods: {
    importSuccess() {
      this.getList()
    },
    handleImport() {
      this.$refs.import.visible = true
    },
    getTreeList() {
      getRequest(this.url.orgList).then((response) => {
        this.orgList = response.data
      })
    },
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.evaluatedName = ''
      this.handleQuery()
    },
    getList() {
      this.loading = true
      this.queryParams.formKey = this.formKey
      if (this.queryParams.evaluated) {
        let evaluateds = this.queryParams.evaluated
        this.queryParams.evaluated = evaluateds[evaluateds.length - 1]
      }
      getRequest(this.url.list, this.queryParams)
        .then((res) => {
          this.dataList = res.data.records
          this.total = res.data.total
        })
        .finally((r) => {
          this.loading = false
        })
    },
    handleUpdate(row) {
      console.log(row)
      this.$refs.modal.edit(row)
    },
    handleAdd() {
      this.$refs.modal.form.formKey = this.formKey
      this.$refs.modal.form.type = this.type
      this.$refs.modal.add()
    },
    submitOk() {
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('是否确认删除？').then(() => {
        this.loading = true
        deleteRequest(this.url.delete, { evaluated: row.evaluated })
          .then((res) => {
            console.log(res)
            this.loading = false
            this.getList()
            this.msgSuccess('删除成功')
          })
          .finally(() => {
            this.loading = false
          })
      })
    },
    handleExport() {
      downloadRelationshipExcel({ formKey: this.formKey, type: this.type }).then((response) => {
        this.download(response, '关联关系导出_' + this.formKey + '_' + new Date().getTime())
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.publish-container {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  background-color: #f7f7f7;
  min-height: 84vh;
  overflow-y: auto;
}

.relation-box {
  width: 80%;
  height: 100%;
  margin: 0 auto;
  //border: 1px solid grey;
  background: white;
}
</style>
